﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utem.Command;

namespace Utem.Server
{
    public partial class FormProjector : Form
    {
        private const int WM_NCHITTEST = 0x84;
        private const int HTCLIENT = 0x1;
        private const int HTCAPTION = 0x2;

        private readonly Image[] ansImages;

        private Timer timer;
        private int counter = 0;

        private int questionNo = 1;

        private Question question = new Question();
        public FormControl.PassDataDelegate PassData { get; set; }


        // Handling the window messages
        protected override void WndProc(ref Message message)
        {
            base.WndProc(ref message);

            if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
            {
                message.Result = (IntPtr)HTCAPTION;
            }
        }

        public FormProjector()
        {
            InitializeComponent();

            // Change timer font.
            var fontsCollection = new PrivateFontCollection();
            fontsCollection.AddFontFile(Application.StartupPath + @"\Fonts\DS-DIGIB.TTF");
            var digiFont = new Font(fontsCollection.Families[0], 28, FontStyle.Bold);
            lbTimer.Font = digiFont;

            // Prepare images.
            ansImages = new Image[4];
            for (var i = 0; i < 4; i++)
            {
                ansImages[i] = Properties.Resources.normal;
            }

            // Initial timer.
            timer = new Timer { Interval = 1000 };
            timer.Stop();
            timer.Tick += timer_Tick;

            // Create Form Control
            var formControl = new FormControl
            {
                PassData = new FormControl.PassDataDelegate(GetData),
                PassQuestion = new FormControl.PassQuestionDelegate(GetQuestion),
                PassStart = new FormControl.PassStartDelegate(StartTimer),
                PassStop = new FormControl.PassStopDelegate(StopTimer),
                PassBonus = new FormControl.PassBonusDelegate(BonusTime),
                PassMark = new FormControl.PassMarkDelegate(SetMark),
                PassWinnerLoser = new FormControl.PassWinnerLoserDelegate(SetWinLose),
                PassInCorrect = new FormControl.PassInCorrectDelegate(ShowAnswer),
                PassCorrectAnswer = new FormControl.PassCorrectAnswerDelegate(SetCorrectAns)
            };

            formControl.Show();

            playerPanel1.ResetAll();

            
        }

        public void ShowAnswer(params int[] pos)
        {
            playerPanel1.SetInCorrect(pos);
            //char tmp;
            //if (question.CorrectAnswer == question.AnsA)
            //SetCorrectAns();
        }

        public void SetWinLose(int pos, string type)
        {
            int[] poss = { pos };
            switch (type)
            {
                case "win":
                    playerPanel1.SetWinner(poss);
                    break;
                case "lose":
                    playerPanel1.SetLoser(poss);
                    break;
            }
        }

        public void GetQuestion(Question question)
        {
            playerPanel1.Reset();
            this.question = question;
            SetQuestion(question, questionNo++);
        }

        public void GetData(string action)
        {
            try
            {
                var temp = action.Substring(0, 3);
                string[] strs;
                switch (temp)
                {
                    case "205":
                        strs = action.Split(':');
                        playerPanel1.Reset();
                        playerPanel1.SetAnswer(int.Parse(strs[2]), strs[1]);
                        //playerPanel1.playerAnsList[int.Parse(strs[2])].Text = strs[1];
                        break;
                    case "199":
                        playerPanel1.ResetAll();
                        scorePanel.Reset();
                        questionNo = 1;
                        break;
                    case "200":
                        strs = action.Split(':');
                        playerPanel1.SetName(int.Parse(strs[2]), strs[1]);
                        //playerPanel1.playerNameList[int.Parse(strs[2])].Text = strs[1];
                        break;
                    case "304":
                    case "308":
                        strs = action.Split(':');
                        scorePanel.SetPoint(int.Parse(strs[1]), int.Parse(strs[2]));
                        scorePanel.IncreasePoint(int.Parse(strs[1]));
                        break;
                }
            }
            catch (Exception)
            {

            }
        }

        public void SetMark(int id, int mark)
        {
            scorePanel.SetPoint(id, mark);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            counter--;
            lbTimer.Text = counter.ToString().PadLeft(2, '0');
            if (counter <= 0)
            {
                timer.Stop();
            }
        }

        public void StartTimer(int secs)
        {
            counter = secs;
            timer.Start();
        }

        public void StopTimer()
        {
            counter = 0;
            timer.Stop();
        }

        public void BonusTime(int secs, bool type)
        {
            var time = int.Parse(lbTimer.Text.ToString());
            secs += time;
            counter = secs;
            timer.Stop();
            timer.Start();
        }

        public void SetQuestion(Question q, int num)
        {
            lbA.Enabled = true;
            lbB.Enabled = true;
            lbC.Enabled = true;
            lbD.Enabled = true;

            lbAnsA.Enabled = true;
            lbAnsB.Enabled = true;
            lbAnsC.Enabled = true;
            lbAnsD.Enabled = true;

            ansImages[0] = Properties.Resources.normal;
            ansImages[1] = Properties.Resources.normal;
            ansImages[2] = Properties.Resources.normal;
            ansImages[3] = Properties.Resources.normal;

            lbQNum.Text = num + "";
            lbQuestion.Text = q.Content;
            lbAnsA.Text = q.AnsA;
            lbAnsB.Text = q.AnsB;
            lbAnsC.Text = q.AnsC;
            lbAnsD.Text = q.AnsD;
        }

        public void SetCorrectAns(char ans)
        {
            switch (ans)
            {
                case 'A':
                    lbB.Enabled = false;
                    lbC.Enabled = false;
                    lbD.Enabled = false;
                    lbAnsB.Enabled = false;
                    lbAnsC.Enabled = false;
                    lbAnsD.Enabled = false;
                    ansImages[0] = Properties.Resources.correct;
                    ansImages[1] = Properties.Resources.wrong;
                    ansImages[2] = Properties.Resources.wrong;
                    ansImages[3] = Properties.Resources.wrong;
                    break;
                case 'B':
                    lbA.Enabled = false;
                    lbC.Enabled = false;
                    lbD.Enabled = false;
                    lbAnsA.Enabled = false;
                    lbAnsC.Enabled = false;
                    lbAnsD.Enabled = false;
                    ansImages[0] = Properties.Resources.wrong;
                    ansImages[1] = Properties.Resources.correct;
                    ansImages[2] = Properties.Resources.wrong;
                    ansImages[3] = Properties.Resources.wrong;
                    break;
                case 'C':
                    lbA.Enabled = false;
                    lbB.Enabled = false;
                    lbD.Enabled = false;
                    lbAnsA.Enabled = false;
                    lbAnsB.Enabled = false;
                    lbAnsD.Enabled = false;
                    ansImages[0] = Properties.Resources.wrong;
                    ansImages[1] = Properties.Resources.wrong;
                    ansImages[2] = Properties.Resources.correct;
                    ansImages[3] = Properties.Resources.wrong;
                    break;
                case 'D':
                    lbA.Enabled = false;
                    lbB.Enabled = false;
                    lbC.Enabled = false;
                    lbAnsA.Enabled = false;
                    lbAnsB.Enabled = false;
                    lbAnsC.Enabled = false;
                    ansImages[0] = Properties.Resources.wrong;
                    ansImages[1] = Properties.Resources.wrong;
                    ansImages[2] = Properties.Resources.wrong;
                    ansImages[3] = Properties.Resources.correct;
                    break;
            }
            Refresh();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawImage(Properties.Resources.qa, 32, 205, 736, 283);

            e.Graphics.DrawImage(ansImages[0], 44, 300, 49, 36);
            e.Graphics.DrawImage(ansImages[1], 44, 348, 49, 36);
            e.Graphics.DrawImage(ansImages[2], 44, 396, 49, 36);
            e.Graphics.DrawImage(ansImages[3], 44, 444, 49, 36);
        }

        private void playerPanel1_Load(object sender, EventArgs e)
        {

        }

    }
}
